﻿// Original author contact info: Owen Emlen (owene_1998@yahoo.com)
// Note: other individuals may also have contributed to this code
// Project hosted on CodePlex.com as of 1/10/2009 at http://www.codeplex.com/EmlenMud
using System.Runtime.Serialization;
using System;
using System.Xml.Serialization;
using System.Threading;
using System.Reflection;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BrainTechLLC.ThreadSafeObjects;

namespace BrainTechLLC
{
    public class WhoCommand : Command, ICommandInfo
    {
        public override string HelpText { get { return "Displays all detectable players who are currently online"; } }

        [NonSerialized, XmlIgnore]
        public static List<string> Words = new List<string>() { "WHO" };

        public override List<string> FirstWords { get { return Words; } }

        [MethodVisible]
        public static List<PerceivedEventAndAudience> PerformMethod(CommandToPerform cmd, IGameThing source, IGameThing target, PropertyData args, PropertyData modifyingArguments)
        {
            List<PerceivedEventAndAudience> events = new List<PerceivedEventAndAudience>();
            GameChar sourceChar = source as GameChar;
            string allParams = (cmd.UserInput == null) ? string.Empty : cmd.UserInput;
            List<MatchAndStrength> items = sourceChar.FindGameThings(allParams, 0, FindOptions.SameRoom | FindOptions.InWorld);

            EventToPerceive evt = CreateObservable(source, items);
            events.Add(new PerceivedEventAndAudience() { Event = evt, Audience = sourceChar.AssociatedID });

            // WORKING HEREHERE

            return events;
        }

        [MethodVisible]
        public static EventToPerceive CreateObservable(IAssociatedID source, List<MatchAndStrength> items)
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("Detectable Players in Game:");

            foreach (var item in items)
            {
                bool self = false;
                GameChar chr = item.MatchedItem as GameChar;
                if (chr != null)
                {
                    if (chr.AssociatedID == source.AssociatedID)
                        self = true;                    
                }

                sb.Append(chr.ShortName.PadRight(14));
                sb.Append(chr.AssociatedID.ToString().PadLeft(8));
                sb.Append(chr.LocationInt.ToString().PadLeft(26));
                sb.AppendLine(self ? " (Self)" : string.Empty);
            }

            return new GeneralEventToPerceive()
            {
                ShowToSource = new Event(sb.ToString(), ViewType.Sense),
                Sources = source.AssociatedID
            };
        }

    }
}
